package araujoCoronelVillamayor.plantasVsZombies.uiWicket;

import araujoCoronelVillamayor.plantasVsZombies.appModelsArena.AlmanaqueAppModel
import araujoCoronelVillamayor.plantasVsZombies.uiWicket.layouts.LayoutPrincipal
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.form.TextField
import org.apache.wicket.model.CompoundPropertyModel
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XButton
import org.uqbar.wicket.xtend.XLink
import org.uqbar.wicket.xtend.XListView

class AlmanaquePage extends LayoutPrincipal {
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods

	private final AlmanaqueAppModel almanaqueAppModel

	new(JardinPage mainPage) {
		super();
		almanaqueAppModel = new AlmanaqueAppModel(mainPage.sesion)
		defaultModel = new CompoundPropertyModel(almanaqueAppModel)

		var formAlmanaque = new Form<AlmanaqueAppModel>("formAlmanaque",
		new CompoundPropertyModel<AlmanaqueAppModel>(almanaqueAppModel))

		agregarGrillaResultados(formAlmanaque)
		this.addChild(formAlmanaque)

		this.addChild(
			new XLink("volverJardin") => [
				onClick = [|responsePage = mainPage]
			])
			almanaqueAppModel.buscar
	}

	/**
	   * Se agrega a la grilla los resultados de la busqueda del Zombie buscado 
	   */
   
	def agregarGrillaResultados(Form<AlmanaqueAppModel> form) {
		val listViewAtaque = new XListView("resultadoBusqueda")
		listViewAtaque.populateItem = [ item |
			item.model = item.modelObject.asCompoundModel
			item.addChild(new Label("nombre"))
			item.addChild(new Label("ataque"))
			item.addChild(new Label("defensa"))
		]

		form.addChild(listViewAtaque)
		form.addChild(new TextField<String>("zombieBuscado"));
		form.addChild(
			new XButton("buscar") => [
				onClick = [ |
					almanaqueAppModel.buscar
				]
			])
		form.addChild(
			new XButton("limpiar") => [
				onClick = [ |
					almanaqueAppModel.limpiar
				]
			])
	}

}
